package com.tduck.cloud.dashboard.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.common.util.SecurityUtils;
import com.tduck.cloud.common.util.ShortIdUtils;
import com.tduck.cloud.dashboard.entity.DashboardPageEntity;
import com.tduck.cloud.dashboard.request.DashboardRequest;
import com.tduck.cloud.dashboard.service.DashboardPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * 仪表盘管理
 *
 * @author tduck-gen
 * @date 2022-10-19 22:01:00
 */
@RestController
@RequestMapping("/dashboard/manage")
public class DashboardPageController {
    @Autowired
    private DashboardPageService dashboardPageService;

    /**
     * 查询仪表盘列表
     */
    @GetMapping("/page")
    public Result<Page<DashboardPageEntity>> queryPage(Page<DashboardPageEntity> page, DashboardPageEntity dashboardPageEntity) {
        dashboardPageEntity.setUserId(SecurityUtils.getUserId());
        LambdaQueryWrapper<DashboardPageEntity> queryWrapper = Wrappers.<DashboardPageEntity>lambdaQuery()
                .like(StrUtil.isNotBlank(dashboardPageEntity.getName()), DashboardPageEntity::getName, dashboardPageEntity.getName())
                .orderByDesc(DashboardPageEntity::getCreateTime);
        Page<DashboardPageEntity> pageResult = dashboardPageService.page(page, queryWrapper);
        return Result.success(pageResult);
    }


    /**
     * 获取仪表盘详细信息
     */
    @GetMapping(value = "/{id}")
    public Result<DashboardPageEntity> getInfo(@PathVariable("id") Long id) {
        return Result.success(dashboardPageService.getById(id));
    }

    /**
     * 获取报仪盘详细信息
     */
    @GetMapping(value = "/key/{key}")
    public Result<DashboardPageEntity> getInfo(@PathVariable("key") String key) {
        return Result.success(dashboardPageService.getOne(Wrappers.<DashboardPageEntity>lambdaQuery()
                .eq(DashboardPageEntity::getShareKey, key)));
    }

    /**
     * 新增仪表盘
     */
    @PostMapping("save")
    public Result<DashboardPageEntity> save(@RequestBody DashboardPageEntity dashboardPage) {
        dashboardPage.setUserId(SecurityUtils.getUserId());
        dashboardPage.setPublishStatus(0);
        dashboardPage.setShareKey(ShortIdUtils.genId());
        boolean save = dashboardPageService.save(dashboardPage);
        return Result.success(dashboardPage);
    }


    /**
     * 修改仪表盘
     */
    @PostMapping("update")
    public Result<DashboardPageEntity> update(@RequestBody DashboardPageEntity dashboardPage) {
        dashboardPageService.updateById(dashboardPage);
        return Result.success(dashboardPage);
    }

    /**
     * 删除仪表盘
     */
    @PostMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(dashboardPageService.removeById(id));
    }


    /**
     * 复制仪表盘
     */
    @PostMapping(value = "/{id}/copy")
    public Result<Boolean> copy(@PathVariable("id") Long id) {
        DashboardPageEntity projectEntity = dashboardPageService.getById(id);
        projectEntity.setName(projectEntity.getName() + "Copy");
        projectEntity.setId(null);
        dashboardPageService.save(projectEntity);
        return Result.success(true);
    }


    /**
     * 获取发布信息
     *
     * @param shareKey 大屏Id
     * @return 发布信息
     */
    @GetMapping("/{shareKey}/publishInfo")
    public Result<Map<Object, Object>> getPublishInfo(@PathVariable String shareKey) {
        DashboardPageEntity dashboardPage = dashboardPageService.getOne(Wrappers.<DashboardPageEntity>lambdaQuery().eq(DashboardPageEntity::getShareKey, shareKey));
        if (!Objects.equals(SecurityUtils.getUserId(), dashboardPage.getUserId())) {
            return Result.success();
        }
        Map<Object, Object> result = MapUtil.newHashMap();
        result.put("id", dashboardPage.getId());
        result.put("status", dashboardPage.getPublishStatus());
        result.put("password", dashboardPage.getAccessPassword());
        return Result.success(result);
    }




    /**
     * 获取是否设置密码
     */
    @GetMapping("/hasPassword")
    public Result<Boolean> hasPassword(@RequestParam String shareKey) {
        DashboardPageEntity dashboardPage = dashboardPageService.getOne(Wrappers.<DashboardPageEntity>lambdaQuery().eq(DashboardPageEntity::getShareKey, shareKey));
        return Result.success(StrUtil.isNotBlank(dashboardPage.getAccessPassword()));
    }


    /**
     * 检查密码是否正确
     *
     */
    @PostMapping("/checkPassword")
    public Result<Boolean> checkPassword(@RequestBody DashboardRequest.CheckPassword checkRequest) {
        DashboardPageEntity dashboardPage = dashboardPageService.getOne(Wrappers.<DashboardPageEntity>lambdaQuery().eq(DashboardPageEntity::getShareKey, checkRequest.getShareKey()));
        return Result.success(Objects.equals(dashboardPage.getAccessPassword(), checkRequest.getPassword()));
    }


}
